package Thread;

import javax.print.DocFlavor;
class Practice{
          private   int count = 0;
          public  void add(){
              synchronized (this){
                  count++;
              }
          }
//       synchronized public void  add(){
//        count++;
//    }
   public  int get(){
        return count;
   }
   public  synchronized  void func(){
        synchronized (Practice.class){

        }
   }
}
public class Demo14 {
    //synchronized的变种写法，可以使用synchronized修饰方法
    public static void main(String[] args) throws InterruptedException {
        StringBuilder stringBuffer;
        Object locker = new Object();
        Practice practice = new Practice();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                practice.add();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                practice.add();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count = "+ practice.get());
    }
    private  static  int count =0;
    public static void main3(String[] args) throws InterruptedException {
        Object locker = new Object();
        Object locker2 = new Object();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                synchronized(locker){
                    count++;
                }
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                synchronized(locker2){
                    count++;
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count = "+count);
    }
    /**
     *
     * @param args
     * @throws InterruptedException
     */
    public static void main2(String[] args) throws InterruptedException {
        Object locker = new Object();
        Thread t1 = new Thread(()->{
            synchronized(locker){
                for (int i = 0; i < 50000; i++) {
                    count++;
                }
            }
        });
        Thread t2 = new Thread(()->{
            synchronized(locker){
                for (int i = 0; i < 50000; i++) {
                    count++;
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count= "+count);
    }

    /**
     * 两个线程，针对同一个对象加锁，才会产生互斥效果
     * （一个线程加上锁了，另一个
     * @param args
     * @throws InterruptedException
     */
    public static void main1(String[] args) throws InterruptedException {
        Object locker = new Object();
       // String s = new String();
        Thread t1 = new Thread(()->{
            for (int i =0; i<50000;i++){
                synchronized(locker){ // 这个synchronized()里的括号填的是用来加锁的对象，
                                      // 要加锁，要解锁，前提是得有有一个锁，
                                     //在Java中 任何一个对象 都可以用作对象。
                                    //比如  Object locker = new Object();  //
                                   // String s = new String();
                    count++;
                }
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                synchronized(locker){
                    count++;
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(" count="+count);
    }
}
